package com.selenium.dinner;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import com.thoughtworks.selenium.DefaultSelenium;

public class TodaysMenu extends Abstract{

	protected static final String BUTTON_UPLOAD_PHOTO = "//div[@class='dinner-complain-button-upload-photo']";
	protected static final String INPUT_COMPLAIN = "//textarea[@class='dinner-input-complain']";
	protected static final String COMPLAIN_BUTTON = "//div[@class='dinner-todays-user-complain-label']";

	public void testTodaysMenu(DefaultSelenium selenium, WebDriver driver) throws Exception {
		
			/* GO to Todays Menu as User */
			waitForElement(selenium, TodaysMenu_Button);
			waitForText(3);
			driver.findElement(By.xpath(TodaysMenu_Button)).click();
			waitForElement(selenium, SLIDER_USER_ToADMIN);
			waitForText(2); 
			driver.findElement(By.xpath(SLIDER_USER_ToADMIN)).click();		

			/* Upload new Image */
			System.out.println("Upload new Image");
			for (int z=1;;z++){
				switchImage(selenium, driver, z);
				driver.findElement(By.xpath("//div[@class='dinner-button-upload-foto-todays-admin-box-container-admin']/input[1]")).sendKeys("/Users/maretska/Pictures/"+z+".png");
				waitForElement(selenium, NAME);
				driver.findElement(By.xpath(NAME)).sendKeys("image "+z);
				waitForElement(selenium, DESCRIPTIONS);
				driver.findElement(By.xpath(DESCRIPTIONS)).sendKeys("image "+z);
				waitForElement(selenium, INPUT_ORDER_ADMIN);
				driver.findElement(By.xpath(INPUT_ORDER_ADMIN)).sendKeys("12"+z+"5");
				waitForElement(selenium, INPUT_PREPARED_ADMIN);
				driver.findElement(By.xpath(INPUT_PREPARED_ADMIN)).sendKeys("12"+z+"5");
				waitForElement(selenium, INPUT_DELIVERED_ADMIN);
				driver.findElement(By.xpath(INPUT_DELIVERED_ADMIN)).sendKeys("12"+z+"5");
				waitForText(1);
				driver.findElement(By.xpath(SAVE_TODAYS_ADMIN)).click();
				waitForText(2);
				driver.findElement(By.xpath("//div[@class = 'dinner-main-content-masking']//div[@class = 'dinner-mask-main dinner-background-mask']//div[@class = 'center']//div[@aria-label = 'customizedAlert']//div[@class = 'alert-customize-main-button']//span[@aria-label='buttonOkay']//div[text()='OK']")).click();
				if (z>=3) break;
			}
			
			/* Test Alert in upload image */
			System.out.println("Test Alert in upload image");
			this.attempt = 0;
			for (int x=1 ;; x++) {
				switchImage(selenium, driver, x);
				waitForElement(selenium, UPLOAD_FOTO_TODAYS_ADMIN);
				driver.findElement(By.xpath(UPLOAD_FOTO_TODAYS_ADMIN)).click();
				if (driver.findElement(By.xpath(okButtonOnPopup)).isDisplayed() && x!=3){
					waitForElement(selenium, okButtonOnPopup);
					driver.findElement(By.xpath(okButtonOnPopup)).click();
					this.attempt = attempt+1;
				}else if (attempt==2){
					waitForElement(selenium, okButtonOnPopup);
					driver.findElement(By.xpath(okButtonOnPopup)).click();
					waitForElement(selenium, DELETE_PHOTO_TODAYS_IMG_LABEL3);
					driver.findElement(By.xpath(DELETE_PHOTO_TODAYS_IMG_LABEL3)).click();
					waitForElement(selenium, BUTTON_YES);
					driver.findElement(By.xpath(BUTTON_YES)).click();
				}
				if (x>=3) break;
			}
			
			/* Upload Image after Delete 3rd image */
			switchImage(selenium, driver, attempt+1);
			waitForElement(selenium, "//div[@class='dinner-button-upload-foto-todays-admin-box-container-admin']");
			driver.findElement(By.xpath("//div[@class='dinner-button-upload-foto-todays-admin-box-container-admin']/input[1]")).sendKeys("/Users/maretska/Pictures/17934691.jpg");
			waitForElement(selenium, NAME);
			driver.findElement(By.xpath(NAME)).sendKeys("Ford Rangers");
			waitForElement(selenium, DESCRIPTIONS);
			driver.findElement(By.xpath(DESCRIPTIONS)).sendKeys("Mobil 4WD Cocok untuk mendaki gunung");
			waitForElement(selenium, INPUT_ORDER_ADMIN);
			driver.findElement(By.xpath(INPUT_ORDER_ADMIN)).sendKeys("1256");
			waitForElement(selenium, INPUT_PREPARED_ADMIN);
			driver.findElement(By.xpath(INPUT_PREPARED_ADMIN)).sendKeys("1257");
			waitForElement(selenium, INPUT_DELIVERED_ADMIN);
			driver.findElement(By.xpath(INPUT_DELIVERED_ADMIN)).sendKeys("1258");
			waitForText(1);
			driver.findElement(By.xpath(SAVE_TODAYS_ADMIN)).click();
			waitForText(2);
			driver.findElement(By.xpath("//div[@class = 'dinner-main-content-masking']//div[@class = 'dinner-mask-main dinner-background-mask']//div[@class = 'center']//div[@aria-label = 'customizedAlert']//div[@class = 'alert-customize-main-button']//span[@aria-label='buttonOkay']//div[text()='OK']")).click();
			waitForElement(selenium, "//div[@class='dinner-todays-user-back-label']");
			
			
			/* Ratings & Reviews */
			System.out.println("Ratings and Reviews Menu fill");
			waitForElement(selenium, SLIDER_USER_ToADMIN);
			waitForText(2); 
			driver.findElement(By.xpath(SLIDER_USER_ToADMIN)).click();
			waitForElement(selenium, "//div[@class='dinner-box-image-container']//div[1]/img");
			driver.findElement(By.xpath("//div[@class='dinner-box-image-container']/div[1]/img")).click();
			waitForElement(selenium, "//div[@class='dinner-todays-user-rating-label']");
			driver.findElement(By.xpath("//div[@class='dinner-todays-user-rating-label']")).click();
			waitForElement(selenium, "//div[@class='dinner-rating-submit-label']");
			driver.findElement(By.xpath("//div[@class='dinner-rating-submit-label']")).click();
			waitForElement(selenium, okButtonOnPopup);
			driver.findElement(By.xpath(okButtonOnPopup)).click();
			waitForElement(selenium, "//textarea[@placeholder='| Write a Review']");
			driver.findElement(By.xpath("//textarea[@placeholder='| Write a Review']")).sendKeys("I love this country so much..");
			waitForElement(selenium, "//div[@class='dinner-rating-stars']/img[3]");
			driver.findElement(By.xpath("//div[@class='dinner-rating-stars']/img[3]")).click();
			waitForElement(selenium, "//div[@class='dinner-rating-submit-label']");
			driver.findElement(By.xpath("//div[@class='dinner-rating-submit-label']")).click();
			waitForElement(selenium, okButtonOnPopup);
			driver.findElement(By.xpath(okButtonOnPopup)).click();
			
			/* Complain */
			waitForElement(selenium, COMPLAIN_BUTTON);
			driver.findElement(By.xpath(COMPLAIN_BUTTON)).click();
			waitForElement(selenium, INPUT_COMPLAIN);
			driver.findElement(By.xpath(INPUT_COMPLAIN)).sendKeys("Complain page for Complain the Menu");
			waitForElement(selenium, BUTTON_UPLOAD_PHOTO);
			driver.findElement(By.xpath("//div[@class='dinner-complain-button-upload-photo']/input")).sendKeys("/Users/maretska/Pictures/4.png");
			waitForElement(selenium, "//div[@class='dinner-complain-button-label-submit']");
			driver.findElement(By.xpath("//div[@class='dinner-complain-button-label-submit']")).click();
			waitForElement(selenium, okButtonOnPopup);
			driver.findElement(By.xpath(okButtonOnPopup)).click();
			
			waitForElement(selenium, SLIDER_USER_ToADMIN);
			waitForText(2); 
			driver.findElement(By.xpath(SLIDER_USER_ToADMIN)).click();
			driver.navigate().back();
	}
	
	public void switchImage (DefaultSelenium selenium, WebDriver driver, int x) throws Exception{
		System.out.println("Start Swicth Image");
		switch (x){
		case 1 :
			// click image one
			waitForElement(selenium, IMAGE_1);
			driver.findElement(By.xpath(IMAGE_1)).click();
			break;
		case 2 :
			// click image two
			waitForElement(selenium, IMAGE_2);
			driver.findElement(By.xpath(IMAGE_2)).click();
			break;
		case 3 :
			// click image three
			waitForElement(selenium, IMAGE_3);
			driver.findElement(By.xpath(IMAGE_3)).click();
			break;
		}
	}
}
